home *** CD-ROM | disk | FTP | other *** search
- Path: cymbal.aix.calpoly.edu!not-for-mail
- From: dstubbs@cymbal.aix.calpoly.edu (Dan Stubbs)
- Newsgroups: comp.lang.c
- Subject: Display an int justified with embedded commas.
- Date: 29 Jan 1996 09:17:08 -0800
- Organization: California Polytechnic State University, San Luis Obispo
- Message-ID: <4eivek$16qu@cymbal.aix.calpoly.edu>
- NNTP-Posting-User: dstubbs@cymbal.aix.calpoly.edu
-
- The following code displays an int right justified in width
- columns (if width is big enough) and with embedded commas.
-
- Is there a better (more concise) way to do this?
-
- /*------------------------------------------------------------------*/
- void comma_print (unsigned int x) {
- if (x > 999) {
- comma_print (x / 1000);
- printf (",%03d", x % 1000);
- } else printf ("%d", x);
- }
-
- void int_print (int x, int width) {
- int len = 1, m = (x < 0),v,n;
- v = n = (m ? -x : x);
-
- while ((n /= 10) > 0) len++;
- len += (len - 1) / 3 + (m ? 1 : 0);
- for (n = len; n < width; n++) putchar (' ');
- if (m) putchar ('-');
- comma_print (v);
- }
- /*------------------------------------------------------------------*/
-